<?php
	class Default_Model_DbTable_UserPreferences extends Zend_Db_Table_Abstract
	{
	    protected $_name = 'user_preferences';
	    protected $_sequence = '_seq_user_preferences';
        
	    protected $_primary = 'pref_id';
	    protected $_pk = 'pref_id';


	    public function get($id, $returnArray = false)
	    {
	        $id = (int)$id;
	    	$row = $this->fetchRow($this->_pk .'= '. $id);
	        if (!$row) {
	            throw new Exception("Count not find row $id");
	        }
	        if ($returnArray == true) {
		        return $row->toArray();
	        }
	        return $row;

	    }

	    public function getRowByFilter($filterArr=array())
	    {
            $filter = $this->select();
            foreach ($filterArr as $field_id=>$field_value) {
                $filter->where($field_id . ' = ?', $field_value);
            }

            $rows = $this->fetchRow($filter);
	        return $rows;
	    }

	    public function getAll($filterArr=array(), $orderArr=array())
	    {
            $filter = $this->select();
            foreach ($filterArr as $field_id=>$field_value) {
                $filter->where($field_id . ' = ?', $field_value);
            }

            foreach ($orderArr as $field_id=>$orderType) {
                $filter->order($field_id .' '. $orderType);
            }//echo $filter;exit;

            $rows = $this->fetchAll($filter);
	        return $rows;
	    }


	    public function getsAsPairs()
	    {
	    	$rows = $this->_db->fetchPairs( $this->select()->from($this->_name, array('fieldname', 'fieldvalue')) );
	        if (!$rows) {
	            throw new Exception("Could not find rows");
	        }
	        return $rows;

	    }


	    public function add($formdata)
	    {
            //print_r($formdata);exit;

            foreach ($formdata['prefs'] as $prefName=>$prefValue) {
                $userPrefs = $this->getAll(array('user_id'=>$formdata['user_id'], 'pref_name'=>$prefName));
                //$existingPrefs = $userPrefs->toArray(); //print_r($existingPrefs);exit;
                //if (!empty($existingPrefs)) {
                    foreach ($userPrefs as $userPref) {
                        $userPref->delete();
                    }
                //}

                $dbData['user_id']      = $formdata['user_id'];
                $dbData['pref_name']    = $prefName;
                $dbData['pref_value']   = (is_array($prefValue) ? implode(',', $prefValue) : $prefValue);
                $dbData['pref_value_type']   = (is_array($prefValue) ? 'array' : 'string');
                //print_r($dbData);exit;
                $this->insert($dbData);
                //$id = $this->_db->lastInsertId($this->_name);
                //return $id;
            }

	    }

	    public function updateRow($id, $data)
	    {
	        $this->update($data, $this->_pk . ' = ' . (int)$id);

	    }

	    public function deleteRow($id)
	    {
	        $this->delete($this->_pk .'=' . (int)$id);
	    }


	}